<script setup>
import { ref } from 'vue'

const props = defineProps(['options', 'default', 'w'])
const emits = defineEmits(['onSelect'])

const cur = ref(props.default)
const open = ref(false)

function handleSelectItem(selected) {
  cur.value = selected
  open.value = false
  emits('onSelect', selected)
}
</script>

<template>
  <div :style="{ width: w }" class="select-container" @mouseleave="open = false">
    <div class="selected-option" @click="open = !open">{{ cur }}</div>
    <Transition name="ani">
      <ul v-show="open" class="options-list">
        <li v-for="(v, i) in options" :key="i" class="option-item" @click="handleSelectItem(v)">
          {{ v }}
        </li>
      </ul>
    </Transition>
  </div>
</template>

<style lang="scss" scoped>
.select-container {
  position: relative;
  height: 2em;
  border: 1px solid #9cb294;
  background-color: var(--bgc-03);
  border-radius: 2px;
  cursor: pointer;
  user-select: none;
}

.selected-option {
  text-align: center;
  height: 100%;
}

.options-list {
  position: absolute;
  top: 102%;
  left: 0;
  right: 0;
  border-top: none;
  max-height: 150px;
  overflow-y: auto;
  background-color: #dcf2e4;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  transition: opacity .3s ease-out;

  .option-item {
    padding: 5px 5px;
    cursor: pointer;

    &:hover {
      background-color: #84c99b;
    }
  }
}


.ani-enter-active,
.ani-leave-active {
  transition: opacity .15s ease-out;
}

.ani-enter-from,
.ani-leave-to {
  opacity: 0;
}
</style>